package com.example.zjx8969.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * Created by zjx8969 on 2018/1/18.
 */

public class NotificationView extends LinearLayout {
    private LinearLayout mNotificationContent;
    //    private TextView mTime;
    private TextView mNotification;
    private ImageView mImageViewAvatar;

    public NotificationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_chat_notification, this);
        mImageViewAvatar =(ImageView) this.findViewById(R.id.chat_user_image);
//        mImageViewAvatar.setOnClickListener(this);
        mNotificationContent = (LinearLayout) this.findViewById(R.id.chat_notification_content);
//        mTime = (TextView) this.findViewById(R.id.chat_notification_time);
        mNotification = (TextView) this.findViewById(R.id.chat_notification_text);

        change(true);
    }


    public void change(boolean isMineOperation) {
        int avatarMarginSide = getContext().getResources().getDimensionPixelSize(R.dimen.chat_message_user_img_margin_left);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageViewAvatar.getLayoutParams();
        RelativeLayout.LayoutParams messageContentLayoutParams = (RelativeLayout.LayoutParams) mNotificationContent.getLayoutParams();
        if (isMineOperation) {
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.setMargins(0, 0, avatarMarginSide, 0);

            messageContentLayoutParams.removeRule(RelativeLayout.RIGHT_OF);
            messageContentLayoutParams.addRule(RelativeLayout.LEFT_OF, R.id.chat_user_image);

        } else {
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.setMargins(avatarMarginSide, 0, 0, 0);


            messageContentLayoutParams.removeRule(RelativeLayout.LEFT_OF);
            messageContentLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.chat_user_image);

        }
        mImageViewAvatar.setLayoutParams(layoutParams);
        mNotificationContent.setLayoutParams(messageContentLayoutParams);
    }

    public String getShowText() {
        if (mNotification == null) return "";
        return mNotification.getText().toString();
    }


    public void setContent(String content) {
        if (mNotification == null) return;
        mNotification.setText(content);
    }


    public void appendText(String content) {
        if (mNotification == null) return;
        mNotification.append(content);
    }
}
